<?php 
/**
 * Plugin Name:Rest Api Sms Sender
 * Plugin URI:http://milad.com.cn
 * Author:Wayne
 * Author URI:http://milad.com.cn
 * Description:Use Rest Api to send sms 
 * 
 */


//POST 到数据表或者自定义的endpoint的话不用nonce的 ？？
require plugin_dir_path( __FILE__ ) . 'ha_register_post_type.php';

add_action('rest_api_init','milad_add_callback_url_endpoint');

function milad_add_callback_url_endpoint() {

  register_rest_route( 
    'milad_sms_api/v1', 
    'recieve_callback',
    array(
    'methods'  => 'POST',
    'callback'  => 'recieve_callback_cb'
  ) );
}


function recieve_callback_cb($request_data) {

  $data = []; 

  //get_params的POST是综合在一起的一个数组  
  $parameters = $request_data->get_params();

  $name = $parameters['name'];
  $password = $parameters['password'];

  if (isset($name) && isset($password)) {

    $data['status']  = 'OK';
  $data['recieved_data']  = array(
    'name'  => $name,
    'password'  => $password,
  );


  $data['message']  = 'You have reached the server';
    
  } else {

    $data['status']  = 'failed';
    $data['message']  = 'Parameters missing'; 


  }

  return $data; 

}